<?php
namespace app\controllers;

use app\models\NormalUser;
use app\models\Enterprise;
use app\models\Feedback;
use app\models\Job;
use app\modules\admin\models\Banner;
use app\modules\admin\models\Config;
use app\modules\admin\models\Tips;
use Codeception\Module\Facebook;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
/*
 * 控制器说明:
 *  前台主页控制器
 *      首页、列表、关于我们、意见反馈、工作列表
 */

class IndexController extends Controller{
    public $defaultAction = 'index';

    public function actionIndex(){
        //显示整个前台页面
        //主页面显示部分用户和企业信息
        $config = $this->getConfig();
        if($config['op_index']==0){
            echo "<p>".$config['comment']."</p>";
            die;
        }else{
            $NormalUser = NormalUser::findBySql('SELECT * FROM NormalUser WHERE published=1 ORDER BY id desc limit 5')->all();
            $Enterprise = Enterprise::findBySql('SELECT * from Enterprise ORDER BY id desc limit 5')->all();
            $Tips = Tips::find()->limit(5)->orderBy(['times'=>SORT_DESC])->all();
            $New_job = Job::find()->limit(16)->orderBy(['id'=>SORT_DESC])->all();
            $Job_list = Job::find()->limit(20)->orderBy(['id'=>SORT_DESC])->all();
            $Banner = Banner::find()->where(['status'=>1])->limit(4)->orderBy(['times'=>SORT_DESC])->all();
            $data = array(
                'NormalUser'=>$NormalUser,
                'Enterprise'=>$Enterprise,
                'Tips' => $Tips,
                'Job_list' => $Job_list,
                'New_job' => $New_job,
                'Banner' => $Banner,
            );
            $this->getView()->title = $config['title'];
            return $this->renderPartial('index',$data);
        }
    }

    public function actionList(){
        //列出用户信息NormalUser 1
        //列出企业信息Enterprise  0
        //列出工作列表Job 2

        $kind = Yii::$app->request->get('kind');
        $this->layout='index';
        $NormalUser = NormalUser::findBySql('SELECT * FROM NormalUser ORDER BY id desc');
        $Enterprise = Enterprise::findBySql('SELECT * FROM Enterprise ORDER BY id desc');
        $Job = Job::findBySql('SELECT * FROM Job WHERE is_examine=1');
        if($kind==1){
            //进入找人才
            $countQuery = clone $NormalUser;
            $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' =>'1']);
            $model = $NormalUser->offset($page->offset)->limit($page->limit)->all();
            return $this->renderPartial('listPerson',['model'=>$model,'page'=>$page]);
        }
        elseif ($kind==2) {
            //进入工作列表
            $countQuery = clone $Job;
            $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '1']);
            $model = $Job->offset($page->offset)->limit($page->limit)->all();
            return $this->renderPartial('listJob', ['model' => $model, 'page' => $page]);
        }
        //进入企业列表
        else{
            $countQuery = clone $Enterprise ;
            $page = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => '1']);
            $model = $Enterprise->offset($page->offset)->limit($page->limit)->all();
            return $this->renderPartial('list', ['model'=>$model,'page'=>$page]);
        }
    }

    public function actionAboutUs(){
        //关于我们平台团队的介绍
        return $this->renderPartial('aboutUs');
    }

    public function actionFeedback(){
        // 意见反馈
        $model = new Feedback();
        $Feedback = Feedback::findBySql('SELECT * FROM Feedback WHERE status=1 ORDER BY `id` DESC limit 7')->all();
        $data = array(
            'model'=>$model,
            'Feedback'=>$Feedback
        );
        if(Yii::$app->request->isPost){
            $post = Yii::$app->request->post('Feedback');
            if($post['name']=='' || $post['content']==''){
                echo '<script>alert("请填写你的信息以及姓名！");location.href("index.php?r=index/feedback");</script>';
                return $this->renderPartial('feedback',$data);
            }//is_null($post['name']) || is_null($post['content'])
            else {
                $MD = new Feedback();
                $MD->name = $post['name'];
                $MD->content = $post['content'];
                $MD->status = 0;
                $MD->save(false);
                echo '<script>alert("您的意见已发表");location.href("index.php?r=index/feedback");</script>';
            }
        }
        return $this->renderPartial('feedback',$data);
    }

    public function getConfig(){
        //获取首页配置
        $data = Config::find()->where(['id'=>1])->asArray()->one();
        return $data;
    }

}